﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "DrawRectElementBehavior.h"
#include "IUiView.h"
#include "IUiDocument.h"
#include "IModelView.h"
#include "IActionInputEventArgs.h"
#include "IControlDefinition.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

DrawRectElementBehavior::DrawRectElementBehavior(IDocument* pDoc)
    : m_pDoc(pDoc)
    , m_onInitActionCallback(nullptr)
    , m_onDestroyActionCallback(nullptr)
    , m_onPropertyPanelEventCallback(nullptr)
    , m_onGetFirstPointCallback(nullptr)
    , m_onGetCornerPointCallback(nullptr)
    , m_onFinishCallback(nullptr)
    , m_onPreviewCallback(nullptr)
{
}

DrawRectElementBehavior::~DrawRectElementBehavior()
{
}

bool DrawRectElementBehavior::SetOnInitActionCallback(OnInitActionCallback callback)
{
    m_onInitActionCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnDestroyActionCallback(OnDestroyActionCallback callback)
{
    m_onDestroyActionCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnPropertyPanelEventCallback(OnPropertyPanelEventCallback callback)
{
    m_onPropertyPanelEventCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnGetFirstPointCallback(OnGetFirstPointCallback callback)
{
    m_onGetFirstPointCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnGetCornerPointCallback(OnGetCornerPointCallback callback)
{
    m_onGetCornerPointCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnFinishCallback(OnFinishCallback callback)
{
    m_onFinishCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnPreviewCallback(OnPreviewCallback callback)
{
    m_onPreviewCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetDefineInputDialogCallback(DefineInputDialogCallback callback)
{
    m_defineInputDialogCallback = callback;
    return true;
}

bool DrawRectElementBehavior::SetOnActionInputEventCallback(OnActionInputEventCallback callback)
{
    m_onActionInputEventCallback = callback;
    return true;
}

bool DrawRectElementBehavior::DefinePromptMsg(std::wstring& promptMsg, const DrawRectStatus drs)
{
    switch (drs)
    {
    case DrawRectStatus::DRS_GETTING_FIRST_POINT:
        promptMsg = GBMP_TR(L"绘制矩形 - 请输入第一角点：");
        break;
    case DrawRectStatus::DRS_GETTING_CORNER_POINT:
        promptMsg = GBMP_TR(L"绘制矩形 - 请输入另一角点：");
        break;
    default:
        break;
    }
    return true;
}

bool DrawRectElementBehavior::DefineCursorType(CursorType& cursorType, const DrawRectStatus drs)
{
    cursorType = CursorType::DrawRectCursor;
    return true;
}

bool DrawRectElementBehavior::DefineInputDialog(std::wstring& title, std::vector<OwnerPtr<IControlDefinition>>& inputItems)
{
    if (m_defineInputDialogCallback)
    {
        DefineInputDialogParams params;
        m_defineInputDialogCallback(params);
        title = params.title;
        inputItems = TransferOwnership(params.inputItems);
        return true;
    }
    else
    {
        return false;
    }
}

bool DrawRectElementBehavior::OnInitAction(IUiView* pCurrentView)
{
    if (m_onInitActionCallback)
    {
        m_onInitActionCallback(pCurrentView);
    }
    return true;
}

bool DrawRectElementBehavior::OnDestroyAction()
{
    if (m_onDestroyActionCallback)
    {
        m_onDestroyActionCallback();
    }
    return true;
}

bool DrawRectElementBehavior::OnActionInputEvent(IActionInputEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");
    if (m_onActionInputEventCallback)
    {
        m_onActionInputEventCallback(pArgs);
    }
    return true;
}

bool DrawRectElementBehavior::OnPropertyPanelEvent(IPropertyPanelEventArgs* pArgs)
{
    if (m_onPropertyPanelEventCallback)
    {
        m_onPropertyPanelEventCallback(pArgs);
    }
    return true;
}

bool DrawRectElementBehavior::OnEnterGettingFirstPointState(IUiView* pCurrentView)
{
    if (m_onGetFirstPointCallback)
    {
        m_onGetFirstPointCallback(pCurrentView);
    }
    return true;
}

bool DrawRectElementBehavior::OnEnterGettingCornerPointState(IUiView* pCurrentView)
{
    if (m_onGetCornerPointCallback)
    {
        m_onGetCornerPointCallback(pCurrentView);
    }
    return true;
}

bool DrawRectElementBehavior::OnDrawRectFinished(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& cornerPt, const ISnap* firstSnap, const ISnap* cornerSnap)
{
    if (m_onFinishCallback)
    {
        m_onFinishCallback(pCurrentView, firstPt, cornerPt);
    }
    return true;
}

bool DrawRectElementBehavior::GetGElementShapeOnPointUpdate(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& pos, OwnerPtr<IGraphicsElementShape>& opGElementShape)
{
    if (m_onPreviewCallback)
    {
        m_onPreviewCallback(pCurrentView, firstPt, pos, opGElementShape);
    }
    return true;
}
